#!/usr/bin/env python
# -*- coding: iso-8859-15 -*-
################################################################### 
# File:
#     $RCSfile: view.py,v $
#
#  State:
#     $State: Exp $
#  
#  Revision:
#     $Revision: 1.7 $
# Id:
#     $Id: view.py,v 1.7 2007/01/02 23:23:05 burny Exp burny $
#
# Log:
#     $Log: view.py,v $
#     Revision 1.7  2007/01/02 23:23:05  burny
#     playing with RCS tags
#
#     Revision 1.6  2007/01/02 23:21:04  burny
#     test
#
#     Revision 1.5  2006/12/31 05:18:26  burny
#     nothin special
#
#     Revision 1.4  2006/12/30 08:36:15  burny
#     added lots of stuff
#
#     Revision 1.3  2006/12/28 17:33:27  burny
#     defined getversion, do_diff and a real main
#
#     Revision 1.2  2006/12/28 15:40:44  burny
#     new test
#
#     Revision 1.1  2006/12/28 15:38:31  burny
#     Initial revision
#
###################################################################

import commands
import os

class MySettings:
    def __init__(self):
	self.rcsdir="RCS"

def getversion(filename):
    file = open(filename)
    for line in file:
	if line[0:4] == "head":
	    return float(line[5:len(line)-2])


header = """
	<div class="page_header">
	<a href="/%s/">%s</a> 

	</div>
	""" %  (os.getcwd().split('/')[-1], os.getcwd().split('/')[-1])
style = """
	<head>
	<script type="text/javascript" src="js/MochiKit/MochiKit.js"></script>
	<script type="text/javascript" src="js/plotkit/Base.js"></script>
	<script type="text/javascript" src="js/plotkit/Layout.js"></script>
	<script type="text/javascript" src="js/plotkit/Canvas.js"></script>
	<script type="text/javascript" src="js/plotkit/SweetCanvas.js"></script>
	<style type="text/css">

	#left {
	    float: left;
	    width: 135px;
	    height: 100%;
	    padding: 40px,40px;
	}
	#right { float: right;
	    height: 100%;
	    width: 1000px;
	    padding: 90px,90px;
	}
	#bottom { 
	    width: 1000px;
	    padding: 00px,1000px,0px, 1000px;
	}
	body {
		font-family: sans-serif; font-size: 12px; border:solid #d9d8d1; border-width:1px;
			margin:10px; background-color:#ffffff; color:#000000;
	}
	a { color:#0000cc; }
	a:hover, a:visited, a:active { color:#880000; }
	div.page_header { height:25px; padding:8px; font-size:18px; font-weight:bold; background-color:#d9d8d1; }
	div.page_header a:visited, a.header { color:#0000cc; }
	div.page_header a:hover { color:#880000; }
	div.page_nav { padding:8px; }
	div.page_nav a:visited { color:#0000cc; }
	div.page_path { padding:8px; border:solid #d9d8d1; border-width:0px 0px 1px}
	div.page_footer { height:17px; padding:4px 8px; background-color: #d9d8d1; }
	div.page_footer_text { float:left; color:#555555; font-style:italic; }
	div.page_body { padding:8px; }
	div.light { padding:10px 10px; }
	div.graph { 
		padding:20px 20px 20px 20px;
	}

	div.title, a.title {
		display:block; padding:10px 10px;
			font-weight:bold; background-color:#edece6; text-decoration:none; color:#000000;
	}
	a.title:hover { background-color: #d9d8d1; }
	div.title_text { padding:6px 0px; border: solid #d9d8d1; border-width:0px 0px 1px; }
	div.log_body { padding:8px 8px 8px 150px; }
	span.age { position:relative; float:left; width:142px; font-style:italic; }
	div.log_link {
		padding:0px 8px;
			font-size:10px; font-family:sans-serif; font-style:normal;
				position:relative; float:left; width:136px;
	}
	div.list_head { padding:6px 8px 4px; border:solid #d9d8d1; border-width:1px 0px 0px; font-style:italic; }
	a.list { text-decoration:none; color:#000000; }
	a.list:hover { text-decoration:underline; color:#880000; }
	a.text { text-decoration:none; color:#0000cc; }
	a.text:visited { text-decoration:none; color:#880000; }
	a.text:hover { text-decoration:underline; color:#880000; }
	table { padding:8px 4px; }
	th { padding:2px 5px; font-size:12px; text-align:left; }
	tr.light:hover { background-color:#edece6; }
	tr.realdark { background-color:#AAAAAA; }
	tr.dark { background-color:#f6f6f0; }
	tr.dark:hover { background-color:#edece6; }
	td { padding:2px 5px; font-size:12px; vertical-align:top; }
	td.link { padding:2px 5px; font-family:sans-serif; font-size:10px; }
	div.pre { font-family:monospace; font-size:12px; white-space:pre; }
	div.diff_info { font-family:monospace; color:#000099; background-color:#edece6; font-style:italic; }
	div.index_include { border:solid #d9d8d1; border-width:0px 0px 1px; padding:12px 8px; }
	div.search { margin:4px 8px; position:absolute; top:56px; right:12px }
	a.linenr { color:#999999; text-decoration:none }
	a.rss_logo {
		float:right; padding:3px 0px; width:35px; line-height:10px;
			border:1px solid; border-color:#fcc7a5 #7d3302 #3e1a01 #ff954e;
				color:#ffffff; background-color:#ff6600;
					font-weight:bold; font-family:sans-serif; font-size:10px;
						text-align:center; text-decoration:none;
	}
	a.rss_logo:hover { background-color:#ee5500; }
	span.tag {
		padding:0px 4px; font-size:10px; font-weight:normal;
			background-color:#ffffaa; border:1px solid; border-color:#ffffcc #ffee00 #ffee00 #ffffcc;
	}
	</style>
	</head>
	<body>
	"""
def numberfile(filename):
    file = open(filename[0:len(filename)-2])
    output=""
    number = 1
    for line in file:
	if number < 10:
	    line = " " + str(number) + "  " + line 
	else: 
	    line = str(number) + "  " + line 
	number+=1
	output+=line
    file.close()
    print output

def doversions(filename):
    out = commands.getoutput('rlog ' + filename)
    first = True
    Begin = True
    output=""
    header = "<tr class ='realdark'><td>revision/lock</td><td>Date/Author/Lines changed</td><td>commit msg</td></tr>"
    color = "light"
    for line in out.split('\n'):
        if line == "----------------------------":
	    if color == "dark":
                color = "light"
            else:
                color = "dark"


	    if first:
		line = "</span></div><div id='right'>" +setupgraph(filename) + "</div><br><div id='bottom'><table cellspacing='0'>"+header+"<tr class='%s'>\n" % color
		first = False
	    else:
		line = "</tr><tr class='%s'>\n" % color

	elif line == "=============================================================================":
	    line = "</tr>\n"
	else:
	    if first == True:
		if Begin == True:
		    
	    	    line = '<div id="left" class="graph"><span>' +line + "\n"
		    Begin = False
		else:
	    	    line = line + "<br>\n"
	    else:
	    	line = "<td>" + line + "</td> \n"
	output+=line
    print output

def numberdiff(version,compareversion,filename):
    out = commands.getoutput('rcsdiff -r1.1 -r'+ str(compareversion) +' -y '+filename)
    output=""
    current = 1
    linenum = 1
    for line in out.split('\n'):
	if current < 06: 
	    line = "   "  + line + "\n"
	    current+=1
	elif linenum < 10:
	    line = " " + str(linenum) + " " + line + "\n"
	    current+=1
	    linenum+=1
	else:
	    line = str(linenum) + " " + line + "\n"
	    current+=1
	    linenum+=1
	output+=line
    print output


def do_diff(filename):
    version = getversion("/home/burny/test/RCS/"+filename)
    compareversion = version
    while compareversion > 1.1:
	numberdiff(version,compareversion,"/home/burny/test/RCS/"+filename)
	compareversion -= .1

def listfiles():
    dir = os.listdir("RCS")
    print "<tr>"
    for file in dir:
	print "<td>" + file[0:len(file)-2] + "</td>"
    print "</tr>"
def setupgraph(filename):
    jsgraph = """
    <script>
var options = {
"IECanvasHTC": "/js/plotkit/iecanvas.htc",
"colorScheme": PlotKit.Base.palette(PlotKit.Base.baseColors()[6]),
"padding": {left: 40, right: 0, top: 5, bottom: 30},
"xTicks": [{v:0, label:"1.1"}, 
{v:1, label:"1.2"}, 
{v:2, label:"1.3"},
{v:3, label:"1.4"},
{v:4, label:"1.5"},
{v:5, label:"1.6"},
{v:6, label:"1.7"},
{v:7, label:"1.8"},
{v:8, label:"1.9"},
{v:9, label:"2.0"},
{v:10, label:"2.1"},
{v:11, label:"2.2"},
{v:12, label:"2.3"},
{v:13, label:"2.4"},
{v:14, label:"2.5"},
{v:15, label:"2.6"},
{v:16, label:"2.7"},
{v:17, label:"2.8"},
{v:18, label:"2.9"},
{v:19, label:"3.0"},
{v:20, label:"3.1"},
{v:21, label:"3.2"},
{v:22, label:"3.3"},
{v:23, label:"3.4"},
{v:24, label:"3.5"},
{v:25, label:"3.6"},
{v:26, label:"3.7"},
{v:27, label:"3.8"},
{v:28, label:"3.9"},
{v:29, label:"4.0"},
],
"drawYAxis": true
};
function drawGraph() {
	var layout = new PlotKit.Layout("pie", officeBlack());
	layout.addDataset("sqrt", %s);
	layout.evaluate();
	var canvas = MochiKit.DOM.getElement("%s");
	var plotter = new PlotKit.SweetCanvasRenderer(canvas, layout, options);
	plotter.render();
    }
    MochiKit.DOM.addLoadEvent(drawGraph);
    </script>
    """

    out = commands.getoutput('rlog '+filename)
    string = "["
    count = 0
    for line in out.split("\n"):
	if line[0:4] == "date":
	    if "lines" in line:
		diff = line[line.index("lines"):len(line)].strip("lines:").strip()
		middle = diff.index(" ")
		first = " "
		last = "0"
		try:
		    first = diff[0:middle].strip("+")
		except:
		    pass
		try:
		    last = diff[middle:len(diff)].strip("-")
		except:
		    pass
		string += "[" + str(count) + ","+ str(int(last)+int(first)) + "] ,"

	    else: 
		string += "[" + str(count) + ",0] ,"
	    count+=1

    string += "]"
    new =jsgraph % (string, filename)


    return new + "<div class='graph'> <div class=''> <canvas id ='%s' height='300' width='300'></canvas></div></div>" % filename
def main():

    dir = os.listdir("/home/burny/test/"+settings.rcsdir)
    print "<html>"
    print style
    print header

    print "<td>RCS repo viewer</td>"
    print "<div><a class='title' href='/rcs/?action=Files'>Files</a>"
    listfiles()
    print "</div>"
    """
    print "<div><a class='title' id='title'  href='/rcs/?action=change'>Total lines Changed</a>"
    for file in dir:
    	setupgraph(file)
    print "</div>"
    """
    print "<div><a class='title' href='/rcs/?action=history'>History</a>"
    #dir = os.listdir(os.getcwd())
    for file in dir:
        #do_diff(file)
        #numberfile("/home/burny/test/"+file)
	doversions(file)

    print "</table>"
    print "</div>"
    print "</div>"
    print "</body>"
    print "</html>"
if __name__ == "__main__":
    settings = MySettings()
    main()

    #setupgraph("view.py")
